<html>
<head><meta charset="utf-8"><title>Const support for asm · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html">Const support for asm</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="197007933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197007933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197007933">(May 09 2020 at 19:24)</a>:</h4>
<p>I'm having a bit of trouble with const support for <code>asm!</code> (that is, taking integer constants and embedding them directly into the asm string).</p>



<a name="197008023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197008023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197008023">(May 09 2020 at 19:24)</a>:</h4>
<p>Basically, <code>ConstValue::try_to_bits_for_ty</code> is failing <a href="https://github.com/rust-lang/rust/pull/69171/files#diff-a5b7af844d26b58c2c9537372b100c90R948" title="https://github.com/rust-lang/rust/pull/69171/files#diff-a5b7af844d26b58c2c9537372b100c90R948">here</a> because the <code>ConstValue</code> is not a <code>Scalar</code>.</p>



<a name="197008133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197008133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197008133">(May 09 2020 at 19:26)</a>:</h4>
<p>This happens because <code>asm!</code> uses constant promotion (following the same logic as <code>#[rustc_args_required_const]</code>), but this always results in a <code>ConstValue::ByRef</code> instead of a <code>ConstValue::Scalar</code> even though I've already checked that the input type is an integer or a float.</p>



<a name="197008188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197008188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197008188">(May 09 2020 at 19:27)</a>:</h4>
<p>The cause seems to be <a href="https://github.com/rust-lang/rust/blob/7c59a81a5fcbaaca311f744cd7c68d99bfbb05d3/src/librustc_mir/const_eval/eval_queries.rs#L201" title="https://github.com/rust-lang/rust/blob/7c59a81a5fcbaaca311f744cd7c68d99bfbb05d3/src/librustc_mir/const_eval/eval_queries.rs#L201">this code</a> in <code>eval_queries.rs</code> which always uses <code>ByRef</code> for promoted constants.</p>



<a name="197008240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197008240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197008240">(May 09 2020 at 19:28)</a>:</h4>
<p>Do you have any suggestions on how const operands should be handled?</p>



<a name="197008305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197008305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197008305">(May 09 2020 at 19:29)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span> since you seem to have written the code that forces <code>ByRef</code> for promoted constants.</p>



<a name="197122028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122028">(May 11 2020 at 09:48)</a>:</h4>
<p>we force <code>ByRef</code>, because promoteds used to be just those that you took references to</p>



<a name="197122069"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122069" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122069">(May 11 2020 at 09:49)</a>:</h4>
<p>we could have some way to differentiate between value and ref promoteds</p>



<a name="197122438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122438">(May 11 2020 at 09:52)</a>:</h4>
<p>less invasive would be to teach all the <code>to_bits</code> logic to read from a <code>ByRef</code></p>



<a name="197122498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122498">(May 11 2020 at 09:53)</a>:</h4>
<p>but that feels like we're running dangerously close to mixing up const values and static memory locations containing const values</p>



<a name="197122648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122648">(May 11 2020 at 09:54)</a>:</h4>
<p>since you know it's always <code>ByRef</code>, you can just match on that and <code>bug!</code> out in all other cases and use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/struct.Allocation.html#method.read_scalar">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/struct.Allocation.html#method.read_scalar</a> to read a scalar from that allocation</p>



<a name="197122742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197122742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197122742">(May 11 2020 at 09:55)</a>:</h4>
<p>or, if you feel like this will only ever allow <code>usize</code>, use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/struct.Allocation.html#method.read_ptr_sized">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/struct.Allocation.html#method.read_ptr_sized</a></p>



<a name="197173186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Const%20support%20for%20asm/near/197173186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Const.20support.20for.20asm.html#197173186">(May 11 2020 at 16:45)</a>:</h4>
<p>@oli Thanks! I got it to work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>